<template>
  <div class="goodsDetail">
    <!-- 面包屑 -->
    <DetailBread :detailObj="detailObj"></DetailBread>
    <!-- 商品图片规格展示 -->
    <div class="w">
      <div class="left">
        <GoodsImg :images="detailObj.mainPictures"></GoodsImg>
        <ul class="goods-sales">
          <li>
            <p>销量人气</p>
            <p>200+</p>
            <p><i class="iconfont icon-task-filling"></i>销量人气</p>
          </li>
          <li>
            <p>商品评价</p>
            <p>400+</p>
            <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
          </li>
          <li>
            <p>收藏人气</p>
            <p>600+</p>
            <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
          </li>
          <li>
            <p>品牌信息</p>
            <p>苏宁电器</p>
            <p><i class="iconfont icon-dynamic-filling"></i>品牌主页</p>
          </li>
        </ul>
      </div>
      <div class="right">
        <!-- 商品名 -->
        <GoodsName
          :name="detailObj.name"
          :desc="detailObj.desc"
          :price="skuObj.price || detailObj.price"
          :oldPrice="skuObj.oldPrice || detailObj.oldPrice"
        ></GoodsName>
        <!-- 商品规格 -->
        <GoodsSku
          :specs="detailObj.specs"
          :skus="detailObj.skus"
          @setSku="setSku"
        ></GoodsSku>
        <!-- 数量组件 -->
        <NumBox :number="number" @cut="cut" @add="add"></NumBox>
        <!-- 加入购物车按钮 -->
        <button class="xtx-button ellipsis" @click="addCart">加入购物车</button>
      </div>
    </div>
    <!-- 同类产品推荐 -->
    <GoodsRelevant :id="detailObj.id"></GoodsRelevant>
    <!-- 商品详情 商品评价 24小时热销 -->
    <GoodsParticulars :detailObjOne="detailObj"></GoodsParticulars>
    <!-- 注意事项 -->
  </div>
</template>

<script>
// 引入请求数据的api
import { getDetail } from "@/utils/Api/DetailApi";
// 引入面包屑
import DetailBread from "@/components/Detail/DetailBread.vue";
// 引入左边图片组件
import GoodsImg from "@/components/Detail/GoodsImg.vue";
// 引入右边商品内容组件
import GoodsName from "@/components/Detail/GoodsName.vue";
// 引入商品推荐
import GoodsRelevant from "./GoodsRelevant.vue";
// 引入商品评价组件
import GoodsParticulars from "./GoodsParticulars.vue";
// 引入规格
import GoodsSku from "./GoodsSku.vue";
// 引入数量
import NumBox from "@/components/utils/NumBox.vue";
// 引入添加购物车的api
import { addCart } from "@/utils/Api/CartApi";
export default {
  components: {
    DetailBread,
    GoodsImg,
    GoodsName,
    GoodsRelevant,
    GoodsParticulars,
    GoodsSku,
    NumBox,
  },
  data() {
    return {
      // 详情页数据
      detailObj: {},
      // 子组件传递过来的规格数据
      skuObj: {},
      // 商品数量
      number: 1,
    };
  },
  mounted() {
    this.getDetailData();
  },
  methods: {
    // 数量减
    cut() {
      this.number--;
      if (this.number <= 1) {
        this.number = 1;
      }
    },
    // 数量加
    add() {
      this.number++;
    },
    // 请求详情页数据
    async getDetailData() {
      let { data } = await getDetail(this.$route.query.id);
      this.detailObj = data.result;
    },
    // 子组件的方法
    setSku(obj) {
      this.skuObj = obj;
    },
    // 点击加入购物车
    async addCart() {
      // 先判断是否选好规格  因为全部选好skuobj中肯定有id
      if (!this.skuObj.skuId) {
        this.$message({
          type: "error",
          text: "请选择完整的规格",
        });
        return false;
      }
      // 判断库存是否还有
      if (this.skuObj.inventory < this.number) {
        this.$message({
          type: "error",
          text: "暂无库存",
        });
        return false;
      }
      // 判断是否登录
      if (
        this.$store.state.User.userInfo &&
        this.$store.state.User.userInfo.token
      ) {
        // 登录了就调用接口添加
        let { data } = await addCart(this.skuObj.skuId, this.number);
        if (data.msg == "操作成功") {
          // 调用更新接口
          await this.$store.dispatch("Cart/asyncGetCart");
          this.$message({
            type: "success",
            text: "加入购物车成功",
          });
        }
      } else {
        // 没登录就添加本地
        // 判重 加入购物车
        let index = this.$store.state.Cart.list.findIndex((item) => {
          return item.skuId == this.skuObj.skuId;
        });
        // console.log(index);
        if (index == -1) {
          // 加入购物车
          let obj = {
            count: this.number,
            name: this.detailObj.name,
            picture: this.detailObj.mainPictures[0],
            selected: false,
            skuId: this.skuObj.skuId,
            price: this.skuObj.price,
            attrText: this.skuObj.skuName,
            isCollect: false,
            stock: this.skuObj.inventory,
            nowPrice: this.skuObj.price,
          };
          // console.log(obj);
          // 调用方法加入购物车
          this.$store.commit("Cart/addList", obj);
          this.$message({
            type: "success",
            text: "加入购物车成功",
          });
        } else {
          // 数量加
          this.$store.commit("Cart/addNumber", { index, count: this.number });
          this.$message({
            type: "success",
            text: "商品已存在,数量增加",
          });
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  display: flex;
  .left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-sales {
  display: flex;
  width: 400px;
  align-items: center;
  text-align: center;
  height: 140px;
  li {
    flex: 1;
    position: relative;
    ~ li::after {
      position: absolute;
      top: 10px;
      left: 0;
      height: 60px;
      border-left: 1px solid #e4e4e4;
      content: "";
    }
    p {
      &:first-child {
        color: #999;
      }
      &:nth-child(2) {
        color: red;
        margin-top: 10px;
      }
      &:last-child {
        color: #666;
        margin-top: 10px;
        i {
          color: #27bb9a;
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: #27bb9a;
          cursor: pointer;
        }
      }
    }
  }
}
.xtx-button {
  border: none;
  background: #27ba9b;
  color: #fff;
  width: 180px;
  height: 50px;
  font-size: 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 20px;
}
</style>
